package com.cngphone.AnFetion;

import java.util.ArrayList;
import java.util.HashSet;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.cngphone.AnFetion.Fetion.TcpSendAndReceive;
import com.cngphone.AnFetion.Fetion.SIPC.SIPCBase;
import com.cngphone.AnFetion.Fetion.SIPC.SerialManager;
import com.cngphone.AnFetion.Fetion.TcpSendAndReceive.IPackageReceivedHandler;

import android.content.Context;

public class FriendManager {
	private ArrayList<FetionFriend> buddies;
	private ArrayList<FetionGroup> groups;
	private HashSet<FetionFriend> chatFriends = new HashSet<FetionFriend>();
	private HashSet<FetionFriend> notifyFriends = new HashSet<FetionFriend>();
	private FetionService ctx;

	public FriendManager(FetionService ctx) {
		this.ctx = ctx;
		this.buddies = new ArrayList<FetionFriend>();
		this.groups = new ArrayList<FetionGroup>();
		groups.add(new FetionGroup(0, "默认分组"));
		this.chatFriends = new HashSet<FetionFriend>();
		this.notifyFriends = new HashSet<FetionFriend>();
	}

	public void newMessageNotify(FetionFriend friend) {
		if (friend != null) {
			notifyFriends.add(friend);
			chatFriends.add(friend);
			MiscUtil.updateMessageNotify(ctx, this, friend);
		}
	}

	public void removeMessageNotify(FetionFriend friend) {
		if (friend != null) {
			notifyFriends.remove(friend);
			MiscUtil.updateMessageNotify(ctx, this, null);
		}
	}

	public void clearMessageNotify() {
		notifyFriends.clear();
		MiscUtil.updateMessageNotify(ctx, this, null);
	}

	public void beginChat(FetionFriend friend) {
		if (friend != null) {
			chatFriends.add(friend);
			friend.lastMessage = null;
		}
	}

	public void endChat(FetionFriend friend) {
		if (friend != null)
			chatFriends.remove(friend);
	}

	public HashSet<FetionFriend> getNotifyFriends() {
		return notifyFriends;
	}

	public HashSet<FetionFriend> getChatFriends() {
		return chatFriends;
	}

	public ArrayList<FetionFriend> getBuddies() {
		return buddies;
	}

	public ArrayList<FetionGroup> getGroups() {
		return groups;
	}

    public FetionFriend getFriend(String uri) {
        if (uri == null || uri.equals("") || buddies == null || buddies.isEmpty()) {
            return null;
        }
        int size = buddies.size();
        for (int i = 0; i < size; i++) {
            FetionFriend ff = buddies.get(i);
            if (ff.Uri.equals(uri)) {
                return ff;
            }
        }
        return null;
    }

	public FetionGroup getGroup(int id) {
		if (groups == null)
			return null;
		for (FetionGroup group : groups) {
			if (group.getId() == id)
				return group;
		}
		return groups.get(0);
	}

	public boolean removeFriend(String uri) {
        int size = buddies.size();
        for (int j = 0; j < size; j++) {
            if (buddies.get(j).Uri.equals(uri)) {
                buddies.remove(j);
                return true;
            }
        }
        return false;
    }

	public boolean moveFriend(FetionFriend friend, int newGroupId) {
		if (friend == null)
			return false;
		FetionGroup oldGroup = getGroup(friend.group);
		if (oldGroup == null)
			return false;
		oldGroup.removeBuddy(friend);
		FetionGroup newGroup = getGroup(newGroupId);
		if (newGroup == null)
			return false;
		newGroup.addBuddy(friend);
		friend.group = newGroupId;
		return true;
	}
}
